﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.IO;

namespace Prism.Core.Storage
{
    public static class Codec
    {
        /// <summary>
        /// 将现存文件系统封装为DAT格式的打包文件。
        /// </summary>
        /// <param name="inputPath">需要封装的文件夹，根据子文件夹的结构归类。</param>
        /// <param name="outputPaths">输出DAT文件的目标文件夹。</param>
        /// <param name="version">指定版本。</param>
        public static void GroupEncode(string inputPath, string outputPath, int version)
        {
            var root = inputPath;

            var storage = new GroupStorageManager();
            storage.ManagerOn();

            var dirs = Directory.EnumerateDirectories(inputPath);

            foreach (var dir in dirs)
            {
                var groupName = Path.GetFileName(dir);
                var allDirectories = AdvancedPath.EnumerateAllDirectories(dir);
                foreach (var _dir in allDirectories)
                {
                    var files = Directory.GetFiles(_dir);
                    foreach (var file in files)
                    {
                        var fs = new FileStream(file, FileMode.Open);
                        var accessName = AdvancedPath.GetRelativePath(file, root);
                        storage.AddData(accessName, fs, new Version() { Value = version }, new Group() { Name = groupName });
                        fs.Close();
                    }
                }
                File.Move(Path.GetFullPath(groupName), Path.Combine(outputPath, groupName));
            }
            File.Move(Path.GetFullPath(storage.ProfilePath), Path.Combine(outputPath, Path.GetFileName(storage.ProfilePath)));
        }


    }
}
